package com.yakuj3.android.comptadroid.fragment;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Switch;

import com.yakuj3.android.comptadroid.R;

public class EditOperationDialogFragment extends DialogFragment {

	
	public interface EditOperationDialogListener {
        public void onEditOperationPositiveClick(DialogFragment dialog);
        public void onEditOperationNegativeClick(DialogFragment dialog);
    }
	
	private EditOperationDialogListener mListener;
	private EditText libele;
	private EditText montant;
	private Switch type;
	
	private String pLibele;
	private String pMontant;
	private boolean pType;
	private long id;
	
	public EditOperationDialogFragment()
	{
		super();
	}
	
	public void setOldLibele(String o)
	{
		this.pLibele = o;
	}
	
	public void setOldMontant(String o)
	{
		
		this.pMontant = o.split(" ")[0];
		if (this.pMontant.charAt(0) == '-')
			this.pMontant = this.pMontant.substring(1);
	}
	
	public void setOldType(boolean o)
	{
		this.pType = o;
	}
	
	public String getLibele()
	{
		return libele.getText().toString();
	}
	
	public String getMontant()
	{
		return montant.getText().toString();
	}
	
	public boolean getType()
	{
		return type.isChecked();
	}
	
	public long getOperationId()
	{
		return this.id;
	}
	
	public void setOperationId(long p)
	{
		this.id = p;
	}
	
	@Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        // Verify that the host activity implements the callback interface
        try {
            // Instantiate the NoticeDialogListener so we can send events to the host
            mListener = (EditOperationDialogListener) activity;
        } catch (ClassCastException e) {
            // The activity doesn't implement the interface, throw exception
            throw new ClassCastException(activity.toString()
                    + " must implement EditOperationDialogListener");
        }
    }
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
	    // Get the layout inflater
	    LayoutInflater inflater = getActivity().getLayoutInflater();
	    
	    View dialogView = inflater.inflate(R.layout.add_operation_dialog_layout, null);

	    libele = (EditText) dialogView.findViewById(R.id.add_operation_libele);
	    montant = (EditText) dialogView.findViewById(R.id.add_operation_montant);
	    type = (Switch) dialogView.findViewById(R.id.add_operation_type);
	    	    
	    libele.setText(pLibele);
	    montant.setText(pMontant);
	    type.setChecked(pType);
	    
	    // Inflate and set the layout for the dialog
	    // Pass null as the parent view because its going in the dialog layout
	    builder.setView(dialogView)
	    // Add action buttons
	           .setPositiveButton("Modifier", new DialogInterface.OnClickListener() {
	               @Override
	               public void onClick(DialogInterface dialog, int id) {
	            	   mListener.onEditOperationPositiveClick(EditOperationDialogFragment.this);
	               }
	           })
	           .setNegativeButton("Annuler", new DialogInterface.OnClickListener() {
	               public void onClick(DialogInterface dialog, int id) {
	                   EditOperationDialogFragment.this.getDialog().cancel();
	               }
	           });      
	    return builder.create();
	}

}
